﻿@{
    Layout = "~/_SiteLayout.cshtml";

    Business.Platform.WorkFlow bworkFlow = new Business.Platform.WorkFlow();
    Business.Platform.DBConnection bdbConn = new Business.Platform.DBConnection();
    string base_TypesOptions = bworkFlow.GetTypeOptions();
    string link_DBConnOptions = bdbConn.GetAllOptions();
    bool isAdd = "1" == Request.QueryString["isadd"];

    string flowID = Request.QueryString["flowid"].IsGuid() && !isAdd ? Request.QueryString["flowid"] : Guid.NewGuid().ToString();
    string defaultManager = Business.Platform.Users.PREFIX + Business.Platform.Users.CurrentUserID.ToString();
}

    <form action="" method="post">
     <div class="mytab" id="mytab">
	    <ul>
		    <li class="mytabli2" for="div_base">基本信息</li>
		    <li class="mytabli1" for="div_data">数据连接</li>
            <li class="mytabli1" for="div_title">标识字段</li>
	    </ul>
        <div style="clear:both;"></div>
     </div>
     <div id="div_base" class="mytab_div" style="display:block;">
     <table cellpadding="0" cellspacing="1" border="0" width="99%" class="formtable">
        <tr>
            <th style="width:100px;">流程ID：</th>
            <td><input type="text" id="base_ID" name="base_ID" readonly="readonly" value="@flowID" class="mytext" style="width:75%"  /></td>
        </tr>
        <tr>
            <th>流程名称：</th>
            <td><input type="text" id="base_Name" name="base_Name" class="mytext" style="width:75%"  /></td>
        </tr>
        <tr>
            <th>流程分类：</th>
            <td>
            <select id="base_Type" name="base_Type" class="myselect" 
            style=""><option value=""></option>@Html.Raw(base_TypesOptions)</select></td>
        </tr>
        <tr>
            <th>管理者：</th>
            <td><input type="text" id="base_Manager" value="@defaultManager" name="base_Manager"  class="mymember" title="选择流程管理者" more="1" user="1" dept="1" station="1" workgroup="1" style="width:75%"  /></td>
        </tr>
        <tr>
            <th>实例管理者：</th>
            <td><input type="text" id="base_InstanceManager" value="@defaultManager" name="base_InstanceManager" class="mymember" title="选择流程实例管理者" more="1" user="1" dept="1" station="1" workgroup="1" style="width:75%"  /></td>
        </tr>
        <tr>
            <th>删除已完成：</th>
            <td>
            <select id="base_RemoveCompleted" name="base_RemoveCompleted" class="myselect" style="width:120px;" >
                <option value="0">不删除</option>
                <option value="1">删除</option>
            </select>
            </td>
        </tr>
        <tr>
            <th>调试模式：</th>
            <td>
                <select class="myselect" id="base_Debug">
                    <option value="0">关闭</option>
                    <option value="1">开启(有调试窗口)</option>
                    <option value="2">开启(无调试窗口)</option>
                </select>
                <input type="text" id="base_DebugUsers" title="选择调试人员" class="mymember" /> //调试人员
            </td>
        </tr>
        <tr>
            <th>备注：</th>
            <td><textarea rows="1" cols="1" id="base_Note" name="base_Note" class="mytext" style="width:90%; height:40px;" ></textarea></td>
        </tr>
     </table>
     </div>
     <div id="div_data" class="mytab_div">
     <table cellpadding="0" cellspacing="1" border="0" width="99%" style="width:99%" class="listtable" id="link_listtable">
        <thead>
            <tr>
                <th style="width:28%">数据库连接</th>
                <th style="width:28%">数据表</th>
                <th style="width:30%">主键</th>
                <th><a href="javascript:link_add();"><img alt="" src="/images/ico/add.gif" style="border:0; vertical-align:middle;" /><span style="vertical-align:middle;">添加</span></a></th>
            </tr>
        </thead>
        <tbody>
            
        </tbody>
     </table>
     </div>
     <div id="div_title" class="mytab_div">
        <table cellpadding="0" cellspacing="1" border="0" width="99%" class="formtable">
            <tr>
                <th style="width:100px;">数据连接：</th>
                <td><select id="title_dbconn" name="title_dbconn" class="myselect" 
                onchange="title_db_change(this)" style="width:400px;" ><option value=""></option>@Html.Raw(link_DBConnOptions)</select></td>
            </tr>
            <tr>
                <th>数据表：</th>
                <td><select id="title_tables" onchange="title_table_change(this)" name="title_tables" class="myselect" style="width:400px;" ></select></td>
            </tr>
            <tr>
                <th>完成标识：</th>
                <td><select id="title_title" name="title_title" class="myselect" style="width:400px;" ></select></td>
            </tr>
        </table>
     </div>
     <div style="width:99%; margin:8px auto 0 auto; text-align:center;">
         <input type="button" class="mybutton" value=" 确 定 " onclick="confirm1(this)" />
         <input type="button" class="mybutton" value=" 取 消 " onclick="new RoadUI.Window().close();" />
     </div>
     </form>
     <script type="text/javascript">
        var link_options = '<option value=""></option>@Html.Raw(link_DBConnOptions)'; //数据连接选项
        var isAdd = '1' == '@Request.QueryString["isadd"]';
        var openerid = '@Request.QueryString["openerid"]';
        var flowID = '@flowID';
        var defaultManager = '@defaultManager';
        var win = new RoadUI.Window();
        var frame = null;

        $(function ()
        {
            var iframes = top.frames;
            for (var i = 0; i < iframes.length; i++)
            {
                if (iframes[i].name == openerid + "_iframe")
                {
                    frame = iframes[i]; break;
                }
            }
            if (frame == null) return;

            if (!isAdd)
            {
                var json = frame.wf_json;
                if (json)
                {
                    $("#base_Name").val(json.name);
                    $("#base_Type").val(json.type);
                    $("#base_Manager").val(json.manager || defaultManager); new RoadUI.Member().setValue($("#base_Manager"));
                    $("#base_InstanceManager").val(json.instanceManager || defaultManager); new RoadUI.Member().setValue($("#base_InstanceManager"));
                    $("#base_RemoveCompleted").val(json.removeCompleted);
                    $("#base_Note").val(json.note);
                    $("#base_Debug").val(json.debug);
                    $("#base_DebugUsers").val(json.debugUsers); new RoadUI.Member().setValue($("#base_DebugUsers"));
                    var databases = json.databases;
                    if (databases)
                    {
                        for (var i = 0; i < databases.length; i++)
                        {
                            link_add(databases[i].link, databases[i].table, databases[i].primaryKey);
                        }
                    }
                    if(json.titleField)
                    {
                        $("#title_dbconn").val(json.titleField.link);
                        $("#title_tables").html(getTables(json.titleField.link, json.titleField.table));
                        $("#title_title").html(getFields(json.titleField.link, json.titleField.table, json.titleField.field));
                    }
                }
            }
        });

        
        function link_add(db,table,field)
        {
            var tableOptions='';
            var fieldOptions='';
            if(db && table)
            {
                tableOptions=getTables(db,table);
            }
            if(db && table && field)
            {
                fieldOptions = getFields(db, table, field);
            }
            var index = $("#link_listtable tbody tr").size() + 1;
            var tr = '<tr>';
            tr += '<td style="background:#ffffff; height:30px;">';
            tr += '<input type="hidden" name="link_index" value="' + index.toString() + '"/>';
            tr += '<select class="myselect" style="width:120px" onchange="link_db_change(this);" id="link_db_' + index.toString() + '" name="link_db_' + index.toString() + '">' + link_options + '</select></td>';
            tr += '<td style="background:#ffffff;"><select class="myselect" style="width:120px" onchange="link_table_change(this)" id="link_table_' + index.toString() + '" name="link_table_' + index.toString() + '">' + tableOptions + '</select></td>';
            tr += '<td style="background:#ffffff;"><select class="myselect" style="width:120px" id="link_key_' + index.toString() + '" name="link_key_' + index.toString() + '">' + fieldOptions + '</select></td>';
            tr += '<td style="background:#ffffff;"><a href="javascript:link_delete(' + index.toString() + ');" class="deletelink">删除</a></td>';
            tr += '</tr>';
            $("#link_listtable tbody").append(tr);
            new RoadUI.Select().init($(".myselect", $("#link_listtable tbody")));
            if(db)
            {
                $("#link_db_" + index.toString()).val(db);
            }
        }
        function link_delete(index)
        {
            $("#link_listtable tbody tr td input[type='hidden']").each(function ()
            {
                if ($(this).val() == index.toString())
                {
                    $(this).parent().parent().remove();
                }
            });
        }
        function link_db_change(obj, table)
        {
            if(!obj || !obj.value) return;
            var html = getTables(obj.value, table);
            $("select", $(obj).parent().next()).html(html);
        }
        function getTables(connid, table)
        {
            var options='<option value=""></option>';
            var tableds = frame.getTables(connid);
            for (var i = 0; i < tableds.length; i++)
            {
                options += '<option value="' + tableds[i].name + '" ' + (tableds[i].name == table ? 'selected="selected"' : '') + '>' + tableds[i].name + '</option>';
            }
            return options;
        }
        function link_table_change(obj, field)
        {
            if (!obj || !obj.value) return;
            var conn = $("select", $(obj).parent().prev()).val();
            $("select", $(obj).parent().next()).html(getFields(conn, obj.value, field));
        }
        function getFields(connid, table, field)
        {
            var options = '<option value=""></option>';
            var fields = frame.getFields(connid, table);
            for (var i = 0; i < fields.length; i++)
            {
                options += '<option value="' + fields[i].name + '" ' + (fields[i].name == field ? 'selected="selected"' : '') + '>' + fields[i].name + '</option>';
            }
            return options;
        }

        function title_db_change(obj, table)
        {
            if (!obj || !obj.value) return;
            $("#title_tables").html(getTables(obj.value, table));
           
        }
        function title_table_change(obj, fields)
        {
            if (!obj || !obj.value) return;
            var conn = $("#title_dbconn").val();
            $("#title_title").html(getFields(conn,obj.value, fields));
        }

       

        function confirm1(but)
        {
            $(but).prop("disabled", true);
            if (isAdd)
            {
                frame.initwf();
            }
            var json = frame.wf_json;
            json.id=flowID;
            json.name = $("#base_Name").val() || '';
            json.type = $("#base_Type").val() || '';
            json.manager = $("#base_Manager").val() || '';
            json.instanceManager = $("#base_InstanceManager").val() || '';
            json.removeCompleted = $("#base_RemoveCompleted").val() || '';
            json.debug = $("#base_Debug").val() || "0";
            json.debugUsers = $("#base_DebugUsers").val() || '';
            json.note = $("#base_Note").val() || '';
            json.databases=[];
            $("input[type='hidden'][name='link_index']").each(function ()
            {
                var index = $(this).val();
                json.databases.push({ link: $('#link_db_' + index).val() || '',
                    linkName: $("#link_db_" + index + " option[value='" + ($('#link_db_' + index).val() || '') + "']").text(),
                    table: $('#link_table_' + index).val() || '',
                    primaryKey: $('#link_key_' + index).val() || ''
                });
            });
            json.titleField = { link: $("#title_dbconn").val() || '',
                table: $("#title_tables").val() || '', 
                field: $("#title_title").val() || '' };

            frame.wf_id=flowID;
            frame.initLinks_Tables_Fields(json.databases);
            new RoadUI.Window().close();
        }
     </script>